package com.twu.biblioteca;

import java.io.BufferedReader;
import java.io.IOException;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: thoughtworker
 * Date: 10/14/13
 * Time: 9:46 AM
 * To change this template use File | Settings | File Templates.
 */
public class LogInAsUserCommand implements Command {

    private ValidateInMap validateInMap;
    private Menu userMenu;
    private BufferedReader reader;
    private ValidateUser validateUser;


    public LogInAsUserCommand(BufferedReader reader, ValidateUser validateUser, Menu userMenu, ValidateInMap validateInMap) {
        this.reader = reader;
        this.validateUser = validateUser;
        this.userMenu = userMenu;
        this.validateInMap = validateInMap;
    }

    @Override
    public void execute() throws IOException {
       String[] usernameAndPassword = tokenizeIntoUsernamePassword(reader.readLine());
       if (validateUser.isValidUser(usernameAndPassword)) {
           userMenu.displayMenuOptions();
           validateInMap.mapToMenu(reader.readLine());
       } else {
           validateInMap.mapToMenu("Invalid");
       }
    }

    public String[] tokenizeIntoUsernamePassword(String userInput) {
        return userInput.split(" ");
    }

}
